package org.come.Jpanel;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.tool.ModerateTask.TaskRoleData;
import com.tool.btn.AutoTaskBtn;
import com.tool.btn.FormsOnOffBtn;
import com.tool.image.ImageMixDeal;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;
import com.xkzhangsan.time.utils.StringUtil;
import org.come.bean.AutoActiveBase;
import org.come.bean.AutoActiveBaseBean;
import org.come.bean.AutoActiveRedisBaseBean;
import org.come.model.AutoTaskBase;
import org.come.mouslisten.HotKeyMouseListen;
import org.come.thread.AutoTaskTimeControlRunnable;
import org.come.thread.TimeControlRunnable;
import org.come.until.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AutoTaskJpanel extends JPanel {

    private JScrollPane paneList, paneDetails;

    private AutoTaskBtn autoTaskBtn;

    private ImageIcon icon1 = new ImageIcon("inkImg/old/background/B458.png");

    private ImageIcon icon2 = new ImageIcon("inkImg/old/background/B459.png");
    private JList<AutoTaskModelJpanel> JlistAutoTaskModelPanel;
    private Map<Integer, AutoTaskModelJpanel> mapAutoTaskModelPanel;

    public Map<Integer, AutoTaskModelJpanel> getMapAutoTaskModelPanel() {
        return mapAutoTaskModelPanel;
    }

    public void setMapAutoTaskModelPanel(Map<Integer, AutoTaskModelJpanel> mapAutoTaskModelPanel) {
        this.mapAutoTaskModelPanel = mapAutoTaskModelPanel;
    }

    public AutoTaskJpanel() {

        setPreferredSize(new Dimension(862, 411));
        setOpaque(false);
        setLayout(null);

        // 关闭按钮事件
        FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/button/1.png", 1, 637);
        offBtn.setBounds(862 - 127, 50, 25, 25);
        this.add(offBtn);
        getPaneList();
        showView();
        getAutoTaskBtn();
    }

    /**
     * 列表鼠标监听
     */
    class PaneListMouse extends MouseAdapter {
        private AutoTaskModelJpanel autoTaskModelJpanel;

        public PaneListMouse(AutoTaskModelJpanel autoTaskModelJpanel) {
            this.autoTaskModelJpanel = autoTaskModelJpanel;
        }

        @Override
        public void mousePressed(MouseEvent e) {
            super.mousePressed(e);
            AutoActiveBase activeBase = autoTaskModelJpanel.getActiveBase();
        }

    }

    /**
     * 展示面板
     */
    public void showView() {
        AutoActiveBaseBean autoActiveBaseBean = UserMessUntil.getAllAutoActive();
        List<AutoActiveBase> allActives = autoActiveBaseBean.getAllautobase();
        String roleId = ImageMixDeal.userimg.getRoleShow().getRole_id().toString();
        String str = Util.readAutoNum(roleId);
        if(str!=null&&!str.equals("")){
            JSON parse = JSONUtil.parse(str);
            if (parse!=null) {
                AutoActiveBaseBean autoActiveBean = GsonUtil.getGsonUtil().getgson().fromJson(parse.toString(), AutoActiveBaseBean.class);
                if(autoActiveBean!=null && autoActiveBean.getAtlist()!=null && autoActiveBean.getAtlist().size()>0){
                    for(AutoActiveBase item:allActives){
                        for(AutoTaskBase item2:autoActiveBean.getAtlist()){
                            if(item.getId() == item2.getId()){
                                item.setGoodNum(item2.getAutoNum());
                            }
                        }
                    }
                }
            }
        }
        if (allActives == null) {
            return;
        }
        mapAutoTaskModelPanel = new HashMap<Integer, AutoTaskModelJpanel>();
        for (int i = 0; i < allActives.size(); i++) {
            AutoActiveBase activeBase = allActives.get(i);
            int sumReceive = TaskRoleData.SumReceive(activeBase.getTasksetId(), 2);
            AutoTaskModelJpanel autoTaskModelJpanel = new AutoTaskModelJpanel();
            autoTaskModelJpanel.setBounds(0, 0 + i * 61, 465, 61);
            autoTaskModelJpanel.showActiveBase(activeBase, sumReceive);
            autoTaskModelJpanel.addMouseListener(new PaneListMouse(autoTaskModelJpanel));
            JlistAutoTaskModelPanel.add(autoTaskModelJpanel);
            mapAutoTaskModelPanel.put(activeBase.getId(), autoTaskModelJpanel);
        }
        JlistAutoTaskModelPanel.setPreferredSize(new Dimension(465, 61 * allActives.size()));

    }

    /**
     * 全体刷新面板
     */
    public void refreshView() {
        AutoActiveBaseBean autoActiveBaseBean = UserMessUntil.getAllAutoActive();
        List<AutoActiveBase> allActives = autoActiveBaseBean.getAllautobase();
        for (int i = 0; i < allActives.size(); i++) {
            AutoActiveBase activeBase = allActives.get(i);
            int sumReceive = TaskRoleData.SumReceive(activeBase.getTasksetId(), 2);
            AutoTaskModelJpanel autoTaskModelJpanel = mapAutoTaskModelPanel.get(activeBase.getTasksetId());
            autoTaskModelJpanel.showActiveBase(activeBase, sumReceive);
        }
    }

    /**
     * 局部活动任务刷新
     */
    public void partRefreshView(int sid) {
        if (sid == 2) {
        } else {
            AutoTaskModelJpanel autoTaskModelJpanel = mapAutoTaskModelPanel.get(sid);
            if (autoTaskModelJpanel == null) {
                return;
            }
            AutoActiveBase activeBase = autoTaskModelJpanel.getActiveBase();
            int sumReceive = TaskRoleData.SumReceive(sid, 2);
            autoTaskModelJpanel.showActiveBase(activeBase, sumReceive);
        }
    }

    private ImageIcon icon;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if (icon == null) {
            icon = new ImageIcon("inkImg/old/background/S341.png");
        }
        g.drawImage(icon.getImage(), 0, 0, 862, 411, this); //411
        g.setColor(Color.white);

        if (AutoTaskTimeControlRunnable.isIsAutoTask()) {
            autoTaskBtn.setIcon(icon2);
        } else {
            autoTaskBtn.setIcon(icon1);
        }
    }

    public JScrollPane getPaneList() {
        if (paneList == null) {
            paneList = new JScrollPane(getJlistAutoTaskModelPanel());
            paneList.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            paneList.getVerticalScrollBar().setUI(new SrcollPanelUI());
            paneList.getVerticalScrollBar().setUnitIncrement(20);
            paneList.getViewport().setOpaque(false);
            paneList.setOpaque(false);
            paneList.setBounds(218, 61, 485, 290);
            paneList.setBorder(BorderFactory.createEmptyBorder());
            paneList.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            this.add(paneList);
        }

        return paneList;
    }

    public AutoTaskBtn getAutoTaskBtn() {
        if (autoTaskBtn == null) {
            autoTaskBtn = new AutoTaskBtn("inkImg/old/background/B458.png", 1, 1, 2,
                    this);
            autoTaskBtn.setBounds(594, 357, 120, 26);

            if (AutoTaskTimeControlRunnable.isIsAutoTask()) {
                autoTaskBtn.setIcon(icon2);
            } else {
                autoTaskBtn.setIcon(icon1);
            }

            this.add(autoTaskBtn);
        }
        return autoTaskBtn;
    }

    public void setPaneList(JScrollPane paneList) {
        this.paneList = paneList;
    }

    public void setPaneDetails(JScrollPane paneDetails) {
        this.paneDetails = paneDetails;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public void setIcon(ImageIcon icon) {
        this.icon = icon;
    }

    public JList<AutoTaskModelJpanel> getJlistAutoTaskModelPanel() {
        if (JlistAutoTaskModelPanel == null) {
            JlistAutoTaskModelPanel = new JList<AutoTaskModelJpanel>();
            JlistAutoTaskModelPanel.setSelectionBackground(new Color(122, 117, 112));
            JlistAutoTaskModelPanel.setSelectionForeground(Color.white);
            JlistAutoTaskModelPanel.setForeground(Color.white);
            JlistAutoTaskModelPanel.setFont(UIUtils.TEXT_HY16);
            JlistAutoTaskModelPanel.removeAll();
            JlistAutoTaskModelPanel.setBackground(UIUtils.Color_BACK); // 设置列表框为透明背景
            DefaultListCellRenderer cellRenderer = new DefaultListCellRenderer() {
                @Override
                public Component getListCellRendererComponent(javax.swing.JList<?> list, Object value, int index,
                                                              boolean isSelected, boolean cellHasFocus) {
                    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                    return this;
                }
            };
            JlistAutoTaskModelPanel.setCellRenderer(cellRenderer);
            JlistAutoTaskModelPanel.setOpaque(false);
        }
        return JlistAutoTaskModelPanel;

    }

    public void setJlistAutoTaskModelPanel(JList<AutoTaskModelJpanel> jlistActivityModelPanel) {
        JlistAutoTaskModelPanel = jlistActivityModelPanel;
    }



}
